<?php

namespace app\admin\controller;

use app\common\controller\Backend;

use think\Db;
use think\Controller;
use think\Request;

/**
 * 
 *
 * @icon user
 */
class Authuser extends Backend
{
    
    /**
     * User模型对象
     */
    protected $model = null;

    public function _initialize()
    {
        parent::_initialize();
        $this->model = model('User');

    }

    public function index()
    {
        //设置过滤方法
//        $this->request->filter(['strip_tags', 'htmlspecialchars']);
        if ($this->request->isAjax())
        {
            //如果发送的来源是Selectpage，则转发到Selectpage
            if ($this->request->request('pkey_name'))
            {
                return $this->selectpage();
            }

            list($where, $sort, $order, $offset, $limit) = $this->buildparams();

            $total = $this->model
                ->where($where)
                ->order($sort, $order)
                ->count();

            $list = $this->model
                ->where($where)
                ->order($sort, $order)
                ->limit($offset, $limit)
                ->select();
            foreach($list as $key=>$val){
                    if($val['status']>=2){
                        $list[$key]['renzheng'] = '认证成功';
                        $list[$key]['kaihu'] = '已开通';
                    }else{
                        if(empty($val['name'])){
                            $list[$key]['renzheng'] = '未申请';
                            $list[$key]['kaihu'] = '未申请';
                        }else{
                            $list[$key]['renzheng'] = '认证失败';
                            $list[$key]['kaihu'] = '申请失败';
                        }
                    }
                    $user_bank = Db::name('user_bank')->where(['userId'=>$val['userId'],'isMain'=>1])->find();
                    if($user_bank){
                        $list[$key]['band'] = '已绑卡';
                        $list[$key]['bandTime'] = $user_bank['createTime'];
                    }else{
                        $list[$key]['band'] = '未绑卡';
                        $list[$key]['bandTime'] = '';
                    }
                    $code = Db::name('sms_code')->field('promotion')->where('phone',$val['phone'])->find();
                    $list[$key]['promotion'] = $code['promotion'];
            }


            $result = array("total" => $total, "rows" => $list);

            return json($result);
        }
        return $this->view->fetch();
    }
    

}
